package com.pfzy.ui.activity.pick;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.pfzy.ui.R;
import com.pfzy.ui.activity.pick.adapter.ArrayWheelAdapter;
import com.pfzy.ui.log.MyLog;
import com.pfzy.ui.utils.PickerViewAnimateUtil;
import com.pfzy.ui.view.wheelview.WheelView;
import com.pfzy.ui.view.wheelview.listener.OnItemSelectedListener;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class PickerDlg10 extends Dialog implements View.OnClickListener {
    private ViewGroup dialogView;// 附加Dialog 的 根View
    private ViewGroup contentContainer;
    private Dialog mDialog;
    private Animation outAnim;
    private Animation inAnim;
    private int animGravity = Gravity.BOTTOM;
    private int gravity = Gravity.CENTER;
    private int textSize = 18;
    private float lineSpacingMultiplier = 1.6f; // 条目间距倍数 默认1.6
    private int textColorOut = 0xFFa8a8a8; // 分割线以外的文字颜色
    private int textColorCenter = 0xFF2a2a2a; // 分割线之间的文字颜色
    private boolean isCenterLabel = false;// 是否只显示中间的label,默认每个item都显示
    public boolean cyclic = false;//是否循环
    private WheelView wv_days;
    private WheelView wv_hours;
    private WheelView wv_minutes;
    private List<DateInfo> mDayList = new ArrayList<>();
    private int mOldDay = 0;
    private int mOldHour = 0;
    private List<String> mDays = new ArrayList<>();
    private List<Integer> mHours = new ArrayList<>();
    private List<Integer> mMinutes = new ArrayList<>();
    private OnTimeSelectListener mOnItemClickListener = null;

    public PickerDlg10(@NonNull Context context) {
        super(context);
        initWindow();
        initAnim();
        initView();
    }

    private void initWindow() {
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        // 如果是对话框模式
        dialogView = (ViewGroup) layoutInflater.inflate(R.layout.layout_basepickerview, null, false);
        // 设置界面的背景为透明
        dialogView.setBackgroundColor(Color.TRANSPARENT);
        // 这个是真正要加载选择器的父布局
        contentContainer = dialogView.findViewById(R.id.content_container);
        // 设置对话框 默认左右间距屏幕30
        params.leftMargin = 30;
        params.rightMargin = 30;
        contentContainer.setLayoutParams(params);

        // 创建对话框
        createDialog();
        //给背景设置点击事件,这样当点击内容以外的地方会关闭界面
        dialogView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }

    protected void initAnim() {
        inAnim = getInAnimation();
        outAnim = getOutAnimation();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.dlg_picker_10, contentContainer);
        // 确定和取消按钮
        Button btnSubmit = contentContainer.findViewById(R.id.dlg_picker_10_btn);
        ImageView btnCancel = contentContainer.findViewById(R.id.dlg_picker_10_close);
        btnSubmit.setOnClickListener(this);
        btnCancel.setOnClickListener(this);

        wv_days = contentContainer.findViewById(R.id.dlg_picker_10_day);
        wv_hours = contentContainer.findViewById(R.id.dlg_picker_10_hour);
        wv_minutes = contentContainer.findViewById(R.id.dlg_picker_10_minute);

        wv_days.setGravity(gravity);
        wv_hours.setGravity(gravity);
        wv_minutes.setGravity(gravity);

        wv_days.setTextSize(textSize);
        wv_hours.setTextSize(textSize);
        wv_minutes.setTextSize(textSize);

        wv_hours.setLabel(getContext().getString(R.string.hours));
        wv_minutes.setLabel(getContext().getString(R.string.minutes));

        wv_days.setTextXOffset(0);
        wv_hours.setTextXOffset(0);
        wv_minutes.setTextXOffset(0);

        wv_days.setCyclic(cyclic);
        wv_hours.setCyclic(cyclic);
        wv_minutes.setCyclic(cyclic);

        wv_days.setDividerColor(Color.YELLOW);
        wv_hours.setDividerColor(Color.YELLOW);
        wv_minutes.setDividerColor(Color.YELLOW);

        wv_days.setDividerType(WheelView.DividerType.FILL);
        wv_hours.setDividerType(WheelView.DividerType.FILL);
        wv_minutes.setDividerType(WheelView.DividerType.FILL);

        wv_days.setLineSpacingMultiplier(lineSpacingMultiplier);
        wv_hours.setLineSpacingMultiplier(lineSpacingMultiplier);
        wv_minutes.setLineSpacingMultiplier(lineSpacingMultiplier);

        wv_days.setTextColorOut(textColorOut);
        wv_hours.setTextColorOut(textColorOut);
        wv_minutes.setTextColorOut(textColorOut);

        wv_days.setTextColorCenter(textColorCenter);
        wv_hours.setTextColorCenter(textColorCenter);
        wv_minutes.setTextColorCenter(textColorCenter);

        wv_days.isCenterLabel(isCenterLabel);
        wv_hours.isCenterLabel(isCenterLabel);
        wv_minutes.isCenterLabel(isCenterLabel);

        wv_days.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                onTimeSelectChanged();
            }
        });
        wv_hours.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                onTimeSelectChanged();
            }
        });
        wv_minutes.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
            }
        });

        initDays();

        List<String> dayList = new ArrayList<>();
        for (int i = 0; i < mDayList.size(); i++) {
            String text = mDayList.get(i).getText();
            dayList.add(text);
        }

        Calendar calendar = Calendar.getInstance();
        int hours, minute;
        calendar.setTimeInMillis(System.currentTimeMillis());
        hours = calendar.get(Calendar.HOUR_OF_DAY);
        minute = calendar.get(Calendar.MINUTE);
        setPicker(dayList, hours, minute);
        mOldDay = 0;
    }

    public void createDialog() {
        if (dialogView != null) {
            mDialog = new Dialog(getContext(), R.style.custom_dialog2);
            mDialog.setCancelable(false);
            mDialog.setContentView(dialogView);
            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(R.style.pickerview_dialogAnim);
                dialogWindow.setGravity(Gravity.CENTER);
            }
        }
    }

    public Animation getInAnimation() {
        int res = PickerViewAnimateUtil.getAnimationResource(this.animGravity, true);
        return AnimationUtils.loadAnimation(getContext(), res);
    }

    public Animation getOutAnimation() {
        int res = PickerViewAnimateUtil.getAnimationResource(this.animGravity, false);
        return AnimationUtils.loadAnimation(getContext(), res);
    }

    private void initDays() {
        mDayList.clear();
        mDayList.add(PickerUtils.getDayText(0, "今天"));
        mDayList.add(PickerUtils.getDayText(1, "明天"));
        mDayList.add(PickerUtils.getDayText(2, "后天"));
        mDayList.add(PickerUtils.getDayText(3));
        mDayList.add(PickerUtils.getDayText(4));
        mDayList.add(PickerUtils.getDayText(5));
        mDayList.add(PickerUtils.getDayText(6));
        mDayList.add(PickerUtils.getDayText(7));
        mDayList.add(PickerUtils.getDayText(8));
        mDayList.add(PickerUtils.getDayText(9));
        mDayList.add(PickerUtils.getDayText(10));
        mDayList.add(PickerUtils.getDayText(11));
        mDayList.add(PickerUtils.getDayText(12));
        mDayList.add(PickerUtils.getDayText(13));
        mDayList.add(PickerUtils.getDayText(14));
    }

    public void setPicker(List<String> items, int h, int m) {
        // 日
        mDays.clear();
        mDays.addAll(items);
        wv_days.setAdapter(new ArrayWheelAdapter(items));// 设置"年"的显示数据
        wv_days.setCurrentItem(0);// 初始化时显示的数据

        // 时
        setHours(h);
        wv_hours.setCurrentItem(0);

        // 分
        setMinutes(m);
        wv_minutes.setCurrentItem(0);
    }

    public void setHours(int hours) {
        mHours.clear();
        for (int i = hours; i < 24; i++) {
            mHours.add(i);
        }
        wv_hours.setAdapter(new ArrayWheelAdapter(mHours));
    }

    public void setMinutes(int minutes) {
        mMinutes.clear();
        for (int i = minutes; i < 60; i++) {
            mMinutes.add(i);
        }
        wv_minutes.setAdapter(new ArrayWheelAdapter(mMinutes));
    }

    public void onTimeSelectChanged() {
        _logd("开始时间选择 原" + mOldDay + "|" + getDays() + "天" + getHours() + "时" + getMinutes() + "分");
        if (mOldDay != getDays()) {
            if (getDays() == 0) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                int hours = calendar.get(Calendar.HOUR_OF_DAY);
                int minute = calendar.get(Calendar.MINUTE);
                setHours(hours);
                setMinutes(minute);
            } else {
                setHours(0);
                setMinutes(0);
            }
            mOldDay = getDays();
        }

        if (getDays() == 0 && mOldHour != getHours()) {
            if (getHours() == 0) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                int minute = calendar.get(Calendar.MINUTE);
                setMinutes(minute);
            } else {
                setMinutes(0);
            }
            mOldHour = getHours();
        }
    }

    public int getDays() {
        return wv_days.getCurrentItem();
    }

    public int getHours() {
        return mHours.get(wv_hours.getCurrentItem());
    }

    public int getMinutes() {
        return mMinutes.get(wv_minutes.getCurrentItem());
    }

    public void showDialog() {
        if (mDialog != null) {
            mDialog.show();
        }
    }

    public void dismissDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dlg_picker_10_close:
                dismissDialog();
                break;
            case R.id.dlg_picker_10_btn:
                if (mOnItemClickListener != null) {
                    int dayindex = getDays();
                    mOnItemClickListener.onTimeSelect(mDayList.get(dayindex), getHours(), getMinutes());
                }
                dismissDialog();
                break;
            default:
                break;
        }
    }

    public void setOnItemClickListener(OnTimeSelectListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public interface OnTimeSelectListener {
        void onTimeSelect(DateInfo day, int hour, int minutes);
    }

    private void _logd(String msg) {
        MyLog.logd("时钟选择器10 : " + msg);
    }
}
